<!-- 第一行统计数据 -->
<template>
	<el-row :gutter="14">
		<el-col :lg="6" :sm="8" :xs="24">
			<div class="sa-wnk">
				<img src="../../static/icon/icon-user.png">
				<div class="sa-wnk-tv">
					<p class="sa-wnk-title">用户</p>
					<p class="sa-wnk-value">{{sta.userCount}}</p>
				</div>
			</div>
		</el-col>
		<el-col :lg="6" :sm="8" :xs="24">
			<div class="sa-wnk">
				<img src="../../static/icon/icon-goods.png">
				<div class="sa-wnk-tv">
					<p class="sa-wnk-title">商品</p>
					<p class="sa-wnk-value">{{sta.goodsCount}}</p>
				</div>
			</div>
		</el-col>
		<el-col :lg="6" :sm="8" :xs="24">
			<div class="sa-wnk">
				<img src="../../static/icon/icon-order.png">
				<div class="sa-wnk-tv">
					<p class="sa-wnk-title">订单</p>
					<p class="sa-wnk-value">{{sta.orderCount}}</p>
				</div>
			</div>
		</el-col>
		<el-col :lg="6" :sm="8" :xs="24">
			<div class="sa-wnk">
				<img src="../../static/icon/icon-money.png">
				<div class="sa-wnk-tv">
					<p class="sa-wnk-title">营业额</p>
					<p class="sa-wnk-value">{{sta.moneyCount}}</p>
				</div>
			</div>
		</el-col>
	</el-row>
</template>

<script>
	module.exports = {
		data() {
			return {
				// 统计数据 
				sta: {
					userCount: 0,
					goodsCount: 0,
					orderCount: 0,
					articleCount: 0,
					commentCount: 0,
					moneyCount: 0,
				},
			}
		},
		methods: {
			// 数值跳动 
			// 对象、属性、结束值、所用时间 
			slowMotion: function(obj, prop, endValue, time) {
				let timeNow = 0;
				let fn = function() {
					// 如果已经接近 or 时间已到，则立即结束 
					var jdz = Math.abs(obj[prop] - endValue);
					if (jdz < 2 || timeNow >= time) {
						// console.log('到点了');
						obj[prop] = endValue;
					} else {
						if (jdz < 100) {
							obj[prop] += 1;
						} else {
							obj[prop] += parseInt((endValue - obj[prop]) / 10); // 平均一下 
						}
						timeNow += 30;
						setTimeout(fn, 30);
					}
				}
				fn();
			},
			// 设置统计数据的数值 
			setStaDataValue: function(staData) {
				for (let key in staData) {
					this.slowMotion(this.sta, key, staData[key], 3000);
				}
			},
			async getTotalAmount() {
				// 获取总金额 
				let res = await axios.get('http://localhost:8080/order/countOrderMoney');
				if (res.data.code === 200) {
					this.sta.moneyCount = res.data.data;
				}
			},
			async getTotalUser() {
				// 获取总用户数 
				let res = await axios.get('http://localhost:8080/user/countUser');
				if (res.data.code === 200) {
					this.sta.userCount = res.data.data;
				}
			},
			async getTotalProduct() {
				// 获取总商品数 
				let res = await axios.get('http://localhost:8080/product/countProduct');
				if (res.data.code === 200) {
					this.sta.goodsCount = res.data.data;
				}
			},
			async getTotalOrder() {
				// 获取总订单数 
				let res = await axios.get('http://localhost:8080/order/countOrder');
				if (res.data.code === 200) {
					this.sta.orderCount = res.data.data;
				}
			}
		},
		created() {
			this.setStaDataValue({
				userCount: this.sta.userCount,
				goodsCount: this.sta.goodsCount,
				orderCount: this.sta.orderCount,
				articleCount: 10368,
				commentCount: 2048,
				moneyCount: this.sta.moneyCount,
			});
			
		},
		mounted() {
			this.getTotalUser();
			this.getTotalAmount();
			this.getTotalProduct();
			this.getTotalOrder();
		}
	}
</script>

<style scoped>
	/* 第一行 */
	.sa-wnk {
		background-color: #FFF;
		border: 1px #ddd solid;
		margin-bottom: 14px;
		min-height: 100px;
		cursor: pointer;
		transition: all 0.3s;
		overflow: hidden;
	}

	.sa-wnk:hover {
		box-shadow: 0 0 20px #999;
	}

	.sa-wnk img {
		float: left;
		line-height: 100px;
		margin: 25px 0px 0 20px;
		width: 50px;
		height: 50px;
		vertical-align: middle;
	}

	.sa-wnk .sa-wnk-tv {
		float: left;
		margin-left: 10px;
		max-width: calc(100% - 100px);
	}

	.sa-wnk-title {
		margin-top: 25px;
		font-size: 16px;
	}

	.sa-wnk-value {
		margin-top: 4px;
		font-size: 24px;
		padding-bottom: 20px;
	}
</style>